package com.woniu.elderly.serve.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.dto.ServeWorkOrderDTO;
import com.woniu.common.entity.DealOrder;
import com.woniu.common.entity.ServePerson;
import com.woniu.common.entity.ServeWorkOrder;
import com.woniu.common.entity.WaresCategory;
import com.woniu.elderly.serve.feignclient.DealOrderFeign;
import com.woniu.elderly.serve.feignclient.ElderlyServiceFeign;
import com.woniu.elderly.serve.feignclient.ElderlyWaresFeign;
import com.woniu.elderly.serve.mapper.ServeWorkOrderMapper;
import com.woniu.elderly.serve.service.ServePersonService;
import com.woniu.elderly.serve.service.ServeWorkOrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;


@Service
public class ServeWorkOrderServiceImpl extends ServiceImpl<ServeWorkOrderMapper, ServeWorkOrder> implements ServeWorkOrderService {
    @Autowired
    private ServePersonService servePersonService;

    @Autowired
    private ElderlyWaresFeign elderlyWaresFeign;

    @Autowired
    private DealOrderFeign dealOrderFeign;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //查询所有工单
    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Page<ServeWorkOrder> findAll(int pageIndex, int pageSize, ServeWorkOrderDTO serveWorkOrderDTO) {
        Wrapper<ServeWorkOrder> wrapper = new LambdaQueryWrapper<>(ServeWorkOrder.class)
                .like(!StringUtils.isEmpty(serveWorkOrderDTO.getServeproject()), ServeWorkOrder::getServeproject, serveWorkOrderDTO.getServeproject())
                .eq(!StringUtils.isEmpty(serveWorkOrderDTO.getState()), ServeWorkOrder::getState, serveWorkOrderDTO.getState())
                ;
        Page<ServeWorkOrder> serveWorkOrderPage = baseMapper.selectPage(new Page<ServeWorkOrder>(pageIndex, pageSize), wrapper);
        serveWorkOrderPage.getRecords().forEach(serveWorkOrder -> {
            serveWorkOrder.setServePerson(servePersonService.findById(serveWorkOrder.getPid()));
        });
        return serveWorkOrderPage;
    }

    //根据id查询工单
    @Override
    public ServeWorkOrder findById(int id) {
        ServeWorkOrder serveWorkOrder = baseMapper.selectById(id);
        serveWorkOrder.setServePerson(servePersonService.findById(serveWorkOrder.getPid()));
        serveWorkOrder.setDealOrder(dealOrderFeign.getDealOrderById(serveWorkOrder.getOid()).getData());
        return serveWorkOrder;
    }

    //添加工单
    @Override
    @GlobalTransactional(name = "fsp_tx_group",rollbackFor = Exception.class)
    public void add(DealOrder dealOrder) {
        //生成工单编号
        String workOrderNumber = "GD-" + IdWorker.getIdStr();
        //获取当前系统时间作为派单时间
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = dateFormat.format(date);

        ServeWorkOrder serveWorkOrder = new ServeWorkOrder();
        serveWorkOrder.setWorkordernumber(workOrderNumber);//工单号

        serveWorkOrder.setServeproject(elderlyWaresFeign.category_getCategory(dealOrder.getCategory_id()).getData().getCname());//服务项目
        serveWorkOrder.setActualpayment(dealOrder.getOrderAmout());//实际付款
        serveWorkOrder.setSendordertime(time);//派单时间
        serveWorkOrder.setState("待服务");//工单状态
        serveWorkOrder.setOid(dealOrder.getOrderId());//订单id
        serveWorkOrder.setPid(1);//服务人员id
        serveWorkOrder.setCid(dealOrder.getUserId());//客户id
        rabbitTemplate.convertAndSend("topicexchange", "sendorder", serveWorkOrder);
    }

    @Override
    public int add(ServeWorkOrder serveWorkOrder) {
        baseMapper.insert(serveWorkOrder);
        return serveWorkOrder.getWid();
    }

    //删除工单
    @Override
    public void remove(int id) {
        baseMapper.deleteById(id);
    }

    //修改工单
    @Override
    public void modify(ServeWorkOrder serveWorkOrder) {
        baseMapper.updateById(serveWorkOrder);
    }
}
